package com.dyness.poi.controller;

import com.dyness.poi.append.AppendCityExtend;
import com.dyness.poi.append.AppendCityService;
import com.dyness.poi.level.CityExtend;
import com.dyness.poi.level.CityLevelService;
import com.dyness.poi.origin.OriginCityExtend;
import com.dyness.poi.origin.OriginCityService;
import com.dyness.sdsp.service.SdspCityService;
import com.dyness.sdsp.service.SysMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author haiqing.gao
 * @date 2024-01-05 14:42
 */
@RestController
@RequestMapping("/city")
@Slf4j
public class CityImportController {

    @Autowired
    private CityLevelService cityLevelService;
    @Autowired
    private OriginCityService originCityService;

    @Autowired
    private AppendCityService appendCityService;

    @Autowired
    private SdspCityService sdspCityService;

    @Autowired
    private SysMessageService sysMessageService;

    @PostMapping("/import")
    public String importCity() {
        log.info("清理历史数据");
        sdspCityService.truncateTable();
        sysMessageService.truncateTable();

        log.info("开始导入");
        List<CityExtend> cityExtends = cityLevelService.loadCityExtend();
        cityLevelService.saveLevelData(cityExtends);
        log.info("层级关系导入完成");
        List<OriginCityExtend> originCityExtends = originCityService.loadCityExtend(cityExtends);
        originCityService.saveLevelData(originCityExtends);
        log.info("原数据迁移完成");
        List<AppendCityExtend> appendCityExtends = appendCityService.loadCityExtend(cityExtends);
        appendCityService.saveLevelData(appendCityExtends);
        log.info("补充数据迁移完成");
        // 导入完成
        // cityExtends
        return "success";
    }

}
